

package jetbrains.buildServer.server.rest.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import jetbrains.buildServer.server.rest.swagger.annotations.ModelDescription;
import jetbrains.buildServer.server.rest.util.BeanContext;
import jetbrains.buildServer.server.rest.util.ValueWithDefault;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author Yegor.Yarko
 * Date: 20/09/2018
 */
@SuppressWarnings({"PublicField", "WeakerAccess"})
@XmlRootElement(name = "operationResult")
@ModelDescription("Represents a relation between a message and a related entity.")
public class OperationResult {
  @XmlElement
  public String message;

  @XmlElement(name ="related")
  public RelatedEntity related;

  @SuppressWarnings("unused")
  public OperationResult() {
  }

  public OperationResult(@NotNull final Data data, @NotNull final Fields fields, @NotNull final BeanContext beanContext) {
    message = ValueWithDefault.decideIncludeByDefault(fields.isIncluded("text"), data.message);
    related = ValueWithDefault.decideIncludeByDefault(fields.isIncluded("related"),
                                                      () -> new RelatedEntity(data.related, fields.getNestedField("related", Fields.LONG, Fields.LONG), beanContext));
  }

  public static class Data {
    @Nullable private final String message;
    @NotNull private final RelatedEntity.Entity related;

    private Data(@Nullable final String message, @NotNull final RelatedEntity.Entity related) {
      this.message = message;
      this.related = related;
    }

    @NotNull
    public static Data createSuccess(@NotNull final RelatedEntity.Entity related) {
      return new Data(null, related);
    }

    @NotNull
    public static Data createError(@NotNull String errorMessage, @NotNull final RelatedEntity.Entity related) {
      return new Data(errorMessage, related);
    }
  }
}